package com.example.demo.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Appointment;
import com.example.demo.entity.AppointmentSign;

import com.example.demo.entity.Exam;
import com.example.demo.mapper.AppointmentSignMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @descreption:
 * @author: xhr
 * @date: 2022/9/27
 */
@RestController
@RequestMapping("/appointmentSign")
public class AppointmentSignController {
    @Resource
    AppointmentSignMapper appointmentSignMapper;

    //学生点击预约，一天一次
    @PostMapping
    public Result<?> save(@RequestBody AppointmentSign appointmentSign) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        AppointmentSign res = appointmentSignMapper.selectOne(new QueryWrapper<AppointmentSign>().eq("user_id", appointmentSign.getUserId()).eq("time", formatter.format(new Date())).eq("appointment_id", appointmentSign.getAppointmentId()));
        if (res == null) {
            appointmentSign.setTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            appointmentSignMapper.insert(appointmentSign);
            return Result.success();
        } else {
            return Result.error("-1", "今日已预约！");
        }

    }

    // 改
//    @PutMapping
//    public Result<?> update(@RequestBody AppointmentSign appointmentSign) {
//        appointmentSignMapper.updateById(appointmentSign);
//        return Result.success();
//    }
    // 删
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        appointmentSignMapper.deleteById(id);
        return Result.success();
    }

    //给某个学生用户看今日的预约情况,需要一表多查
    @GetMapping("/page/front/{userId}")
    public Result<?> todayAppointment(@PathVariable Integer userId) {
        //当天的预约数量
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//        List<AppointmentSign> appointmentSignList = appointmentSignMapper.selectList(new QueryWrapper<AppointmentSign>().eq("time", formatter.format(new Date())));
//        List<Question> list =paperQuestionService.selectQuestions(paperId);
        List<Appointment> list = appointmentSignMapper.selectAppointment(userId, formatter.format(new Date()));
        return Result.success(list);
    }

    //全(页)查
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "10") Integer pagesize,
                              @RequestParam(defaultValue = "1") Integer pagenum,
                              @RequestParam(defaultValue = "") String today,
                              @RequestParam(defaultValue = "1") Integer role,
                              @RequestParam(defaultValue = "") Integer counselorId
    ) {
        LambdaQueryWrapper<AppointmentSign> wrapper;
        if (role == 1) {
            wrapper = Wrappers.<AppointmentSign>lambdaQuery();
        } else {
            wrapper = Wrappers.<AppointmentSign>lambdaQuery().eq(AppointmentSign::getCounselorId, counselorId);
        }
        if (StrUtil.isNotBlank(today)) {
            wrapper.like(AppointmentSign::getTime, today);
        }
        return Result.success(appointmentSignMapper.selectPage(new Page<>(pagenum, pagesize), wrapper));
    }

}
